var qlmUtil = {
    toast: function(msg, duration) {
        duration = isNaN(duration) ? 2000 : duration;
        var m = document.createElement('div');
        m.innerHTML = msg;
        m.style.cssText = "max-width:100%;min-width: 150px;padding:10px 10px;color: rgb(255, 255, 255);line-height: 24px;text-align: center;border-radius: 4px;position: fixed;top: 50%;left: 50%;transform: translate(-50%, -50%);z-index: 999999;background: rgba(0, 0, 0,.7);font-size: 16px;";
        document.body.appendChild(m);
        setTimeout(function() {
            var d = 0.3;
            m.style.webkitTransition = '-webkit-transform ' + d + 's ease-in, opacity ' + d + 's ease-in';
            m.style.opacity = '0';
            setTimeout(function() {
                document.body.removeChild(m)
            }, d * 1000);
        }, duration);
    },
    getQuery: function(key) {
        try {
            var reg = new RegExp("(^|&)" + key + "=([^&]*)(&|$)");
            //如果地址栏中出现中文则进行编码
            var r = encodeURI(window.location.search).substr(1).match(reg);
            if (r != null) {
                //将中文编码的字符重新变成中文
                return decodeURI(unescape(r[2]));
            }
            return null;
        } catch (e) {
            console.error('getQueryStringError！', key, window.location.search)
            return "";
        }
    },
    getCookie: function(name) {
        var strcookie = document.cookie;//获取cookie字符串
        var arrcookie = strcookie.split("; ");//分割
        //遍历匹配
        for (var i = 0; i < arrcookie.length; i++) {
            var arr = arrcookie[i].split("=");
            if (arr[0] == name) {
                return arr[1];
            }
        }
        return "";
    },
    isPhone: function(phone) {
        try {
            return /^1[3456789]\d{9}$/.test(phone)
        } catch (err) {
            return false
        }
    },
    // 判断是否是微信浏览器的函数
    isWeiXin: function() {
        //window.navigator.userAgent属性包含了浏览器类型、版本、操作系统类型、浏览器引擎类型等信息，这个属性可以用来判断浏览器类型
        var ua = window.navigator.userAgent.toLowerCase();
        //通过正则表达式匹配ua中是否含有MicroMessenger字符串
        return ua.match(/MicroMessenger/i) == 'micromessenger';
    },
    isAndroid: function() {
        var u = navigator.userAgent;
        return u.indexOf('Android') > -1 || u.indexOf('Linux') > -1;
    },
    isIOS: function() {
        var u = navigator.userAgent;
        return !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/); //ios终端
    },
    // 与原生交互
    eventBusForNative(func, params, cb) {
        if (qlmUtil.isIOS()) {
            eventBusForIOS((bridge) => {
                bridge.callHandler(func, params, cb);
            });
        } else if (qlmUtil.isAndroid()) {
            eventBusForAndroid(func, params);
            window["func_" + func] = cb;
        }

        // IOS app通讯
        function eventBusForIOS(callback) {
            // @ts-ignore
            if (window.WebViewJavascriptBridge) {
                // @ts-ignore
                return callback(WebViewJavascriptBridge);
            }
            // @ts-ignore
            if (window.WVJBCallbacks) {
                // @ts-ignore
                return window.WVJBCallbacks.push(callback);
            }
            // @ts-ignore
            window.WVJBCallbacks = [callback];
            const WVJBIframe = document.createElement("iframe");
            WVJBIframe.style.display = "none";
            WVJBIframe.src = "wvjbscheme://__BRIDGE_LOADED__";
            document.documentElement.appendChild(WVJBIframe);
            setTimeout(function() {
                document.documentElement.removeChild(WVJBIframe);
            }, 0);
        }

        // Android app通讯
        function eventBusForAndroid(func, params) {
            try {
                window.JavaInterfaceDialog[func](params);
            } catch (error) {
                console.error(error);
            }
        }
    }
}
